package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.CustomerCourseClassMapper;
import com.hamster.crmpre.mapper.CustomerCourseClassTimeMapper;
import com.hamster.crmpre.mapper.CustomerMapper;
import com.hamster.crmpre.mapper.ProjectMapper;
import com.hamster.crmpre.pojo.Customer;
import com.hamster.crmpre.pojo.CustomerCourseClass;
import com.hamster.crmpre.pojo.CustomerCourseClassTime;
import com.hamster.crmpre.pojo.Project;
import com.hamster.crmpre.service.ApplicationStatusService;
import com.hamster.crmpre.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2020/1/2 15:22
 * @description：课程报名情况
 * @modified By：
 * @version:
 */
@Service
public class ApplicationStatusServiceImpl implements ApplicationStatusService {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerCourseClassMapper customerCourseClassMapper;
    @Autowired
    private CustomerCourseClassTimeMapper customerCourseClassTimeMapper;

    /**
     * 获得类别为1和2的项目id和项目名
     * @return
     */
    @Override
    public List<Map<String, String>> getStatus() {
        List<Map<String, String>> lists = new ArrayList<>();
        //获得项目类别为1和2的所有项目
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "title");
        queryWrapper.eq("del","0").in("category_id",1,2);

        List<Project> projects = projectMapper.selectList(queryWrapper);
        //存入List<Map<String, String>>之中返回
        for (int i = 0; i < projects.size(); i++) {
            Project project = projects.get(i);
            Map<String, String> map = new HashMap<>();
            map.put("id",project.getId());
            map.put("name",project.getTitle());
            lists.add(map);
        }
        return lists;
    }

    /**
     * 获取课程报名情况详细列表
     * @return
     */
    @Override
    public List<Map<String, Object>> getCourseClass(String id,Date startTime, Date endTime) {
        List<Map<String, Object>> objList = new ArrayList<>();

        //根据 项目的id ，查询项目下面的课程id的list
        Project project = projectMapper.selectById(id);
        List<String> courses = JsonUtils.parseList(project.getCourse(), String.class);
        //查询自己负责的客户
        String userId = LoginInterceptor.getLoginUser().getUserId();
        List<String> customerIdList = customerMapper.queryCustomerId(userId);

        List<CustomerCourseClass> classList = new ArrayList<>();
        //1.循环遍历拿出 课程集合
        for (String cours : courses) {
            for (String cid: customerIdList) {
                QueryWrapper<CustomerCourseClass> customerCourseClassQueryWrapper = new QueryWrapper<>();
                customerCourseClassQueryWrapper.eq("customer_id",cid).eq("course_id",cours);
                List<CustomerCourseClass> list = customerCourseClassMapper.selectList(customerCourseClassQueryWrapper);
                if (list.size() > 0){
                    CustomerCourseClass customerCourseClass = list.get(0);
                    if (customerCourseClass != null){
                        classList.add(customerCourseClass) ;
                    }
                }


            }
        }
      //  System.out.println(classList);
        //2.根据 课程id + 时间 筛选出最终结果
        for (CustomerCourseClass aClass : classList) {
            QueryWrapper<CustomerCourseClassTime> customerCourseClassTimeQueryWrapper = new QueryWrapper<CustomerCourseClassTime>();
            customerCourseClassTimeQueryWrapper.eq("course_class_id",aClass.getId());
            customerCourseClassTimeQueryWrapper.ge("start_time",startTime);
            customerCourseClassTimeQueryWrapper.lt("start_time",endTime);
            List<CustomerCourseClassTime> customerCourseClassTimes = customerCourseClassTimeMapper.selectList(customerCourseClassTimeQueryWrapper);
            aClass.setTimeList(customerCourseClassTimes);

            QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id","phone","name");
            queryWrapper.eq("id",aClass.getCustomerId());
            Customer customer = customerMapper.selectOne(queryWrapper);
            Map<String, Object> stringObjectMap = new HashMap<>();
            stringObjectMap.put("customerId",customer.getId());
            stringObjectMap.put("phone",customer.getPhone());
            stringObjectMap.put("name",customer.getName());
            stringObjectMap.put("course",aClass);//课程实体对象
            objList.add(stringObjectMap);
        }
        return objList; //返回结果
    }


}
